﻿using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;

namespace UI
{
    public class RecycleBreakupPanel : MonoBehaviour, ISubContent
    {
        public List<CommonIcon> items;
        public List<UITweener> tweeners;

        public GameObject effect;
        public Animator wheelAni;
        private int recycleTime;//回收、分解次数

        public const string breakupAudio = "recycle_decomposed";
        
        public void OnBreakupIconClick()
        {
            if (StaticData.IsRecycleBreakupSelect())
            {
                PageManager.Instance.OpenPage("RecycleBreakupSelectPage", "type=" + StaticData.curRecycleBreakSelectType);
            }
            else
            {
                PageManager.Instance.OpenPage("RecycleBreakupSelectPage", "");
            }
        }

        public void OnEquipBreakupButtonClick()
        {
            StaticData.ClearRecycleBreakupSelect();
            var uids = StaticData.playerData.InstPlayerData.EquipBreakupAutoSelect(4);
            if (uids.Count == 0)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("recycle_auto_add_equip_warning"));
                Refresh();
                return;
            }
            for (int i = 0; i < uids.Count; ++i)
            {
                StaticData.recycleBreakupSelect[i] = uids[i];
            }
            StaticData.curRecycleBreakSelectType = 1;
            Refresh();
        }
        public void OnHeroBreakupButtonClick()
        {
            StaticData.ClearRecycleBreakupSelect();
            var uids = StaticData.playerData.InstPlayerData.HeroBreakupAutoSelect(4);
            if (uids.Count == 0)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("recycle_auto_add_hero_warning"));
                Refresh();
                return;
            }
            for (int i = 0; i < uids.Count; ++i)
            {
                StaticData.recycleBreakupSelect[i] = uids[i];
            }
            StaticData.curRecycleBreakSelectType = 0;
            Refresh();
        }

        public void OnBreakupConfirmButtonClick()
        {
            if (!StaticData.IsRecycleBreakupSelect())
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("recycle_select_count_warning"));
                return;
            }

            bool isHaveOrange = false;
            for (int i = 0; i < 4; ++i)
            {
                if (!string.IsNullOrEmpty(StaticData.recycleBreakupSelect[i]))
                {
                    if (StaticData.curRecycleBreakSelectType == 0)
                    {
                        var uiHero = StaticData.playerData.GetUIHero(StaticData.recycleBreakupSelect[i]);
                        if (uiHero.heroData.quality >= (int)QualityValue.YELLOW)
                        {
                            isHaveOrange = true;
                        }
                    }
                    else if(StaticData.curRecycleBreakSelectType == 1)
                    {
                        var uiEquipment = StaticData.playerData.GetUIEquipment(StaticData.recycleBreakupSelect[i]);
                        if (uiEquipment.equipData.quality >= (int)QualityValue.YELLOW)
                        {
                            isHaveOrange = true;
                        }
                    }

                }
            }
            if (isHaveOrange)
            {
                if (StaticData.curRecycleBreakSelectType == 0)
                {
                    BoxManager.CreateMessageBoxWithCofirmAndCancelButton(TableTextBlo.GetText("recycle_breakup_hero_warning"), true, (b) => {
                        if (b)
                        {
                            ComfirmBreakup();
                        }
                    }, TableTextBlo.GetText("recycle_breakup_hero"));

                }
                else if (StaticData.curRecycleBreakSelectType == 1)
                {
                    BoxManager.CreateMessageBoxWithCofirmAndCancelButton(TableTextBlo.GetText("recycle_breakup_equip_warning"), true, (b) => {
                        if (b)
                        {
                            ComfirmBreakup();
                        }
                    }, TableTextBlo.GetText("recycle_breakup_equip"));

                }
            }
            else
            {
                ComfirmBreakup();
            }



        }

        IEnumerator _ConfirmBreakup()
        {
            AudioManager.Instance.PlayUIAudio(breakupAudio);
            var mask = Instantiate(Resources.Load("prefab/TopMask")) as GameObject;
            mask.transform.parent = GameObject.Find("/UI Root").transform;
            mask.gameObject.SetActive(true);

            effect.SetActive(true);
            wheelAni.Play(RecyclePage.wheelAniSpeedupName);
            yield return new WaitForSeconds(2.7f);
            effect.SetActive(false);
            PageManager.Instance.OpenPage("RecycleRewardPage", "type=0");
            wheelAni.Play(RecyclePage.wheelAniNormalName);

            Destroy(mask.gameObject);
        }

        void ComfirmBreakup()
        {
            if (StaticData.curRecycleBreakSelectType == 1)
            {
                ProtoBuf.Message.CSEquipDecompose msg = new ProtoBuf.Message.CSEquipDecompose();
                for (int i = 0; i < 4; ++i)
                {
                    if (!string.IsNullOrEmpty(StaticData.recycleBreakupSelect[i]))
                    {
                        msg.guid.Add(StaticData.recycleBreakupSelect[i]);
                    }
                }
                NetAPI.SendHttp(ProtoBuf.Message.OpDefine.CSEquipDecompose, msg, (pack, b) =>
                {
                    if (b)
                    {
                        ProtoBuf.Message.SCEquipDecompose _msg = pack.kBody as ProtoBuf.Message.SCEquipDecompose;
                        StaticData.playerData.Delete(_msg.cost);
                        StaticData.playerData.AddResultObj(_msg.get);
                        StaticData.ClearRecycleBreakupSelect();
                        StaticData.recycleReward = _msg.get;
                        Refresh();

                        StartCoroutine(_ConfirmBreakup());
                    }
                });
            }
            else if (StaticData.curRecycleBreakSelectType == 0)
            {
                ProtoBuf.Message.CSWarriorDecompose msg = new ProtoBuf.Message.CSWarriorDecompose();
                for(int i = 0; i < 4; ++i)
                {
                    if (!string.IsNullOrEmpty(StaticData.recycleBreakupSelect[i]))
                    {
                        msg.guid.Add(StaticData.recycleBreakupSelect[i]);
                    }
                }
                NetAPI.SendHttp(ProtoBuf.Message.OpDefine.CSWarriorDecompose, msg, (pack, b) => {
                    if (b)
                    {
                        ProtoBuf.Message.SCWarriorDecompose _msg = pack.kBody as ProtoBuf.Message.SCWarriorDecompose;
                        StaticData.playerData.Delete(_msg.cost);
                        StaticData.playerData.AddResultObj(_msg.get);
                        StaticData.ClearRecycleBreakupSelect();
                        StaticData.recycleReward = _msg.get;
                        Refresh();
                        StartCoroutine(_ConfirmBreakup());
                    }
                });
            }
        }

        public void OnOpenSubPageTweenOver()
        {

        }

        void Refresh()
        {
            switch (StaticData.curRecycleBreakSelectType)
            {
                case 0:
                    for (int i = 0; i < 4; ++i)
                    {
                        if (!string.IsNullOrEmpty(StaticData.recycleBreakupSelect[i]))
                        {
                            var hero = StaticData.playerData.InstPlayerData.GetUIHero(StaticData.recycleBreakupSelect[i]);
                            items[i].gameObject.SetActive(true);
                            items[i].SetData(hero.heroId, false);
                        }
                        else
                        {
                            items[i].gameObject.SetActive(false);
                        }
                    }
                    break;
                case 1:
                    for (int i = 0; i < 4; ++i)
                    {
                        if (!string.IsNullOrEmpty(StaticData.recycleBreakupSelect[i]))
                        {
                            var equip = StaticData.playerData.InstPlayerData.GetUIEquipment(StaticData.recycleBreakupSelect[i]);
                            items[i].gameObject.SetActive(true);
                            items[i].SetData(equip.equipId, false, -1, equip);
                        }
                        else
                        {
                            items[i].gameObject.SetActive(false);
                        }
                    }
                    break;
            }

        }

        bool isShow;
        public void CloseTab()
        {
            gameObject.SetActive(false);
        }

        public bool IsShow()
        {
            return isShow;
        }

        public void OnPageClose()
        {
            StaticData.ClearRecycleBreakupSelect();
        }

        public void OnRemoveCoverPage()
        {
            Refresh();
        }

        public void ReShowTab()
        {
            Refresh();
        }

        public void SetShow(bool isShow)
        {
            this.isShow = isShow;
        }

        public void ShowTab()
        {
            gameObject.SetActive(true);
            Refresh();
            wheelAni.Play(RecyclePage.wheelAniNormalName);
        }


    }
}